package ca.paulshin.yunatube.search;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import ca.paulshin.yunatube.R;
import ca.paulshin.yunatube.common.Constants;

public class Search extends Dialog {
	public Search(final Activity activity) {
		super(activity, R.style.Theme_YunaTubeDialog);
		this.setContentView(R.layout.dialog_search);
		this.setCancelable(true);

		ImageButton ui_youtube = (ImageButton) findViewById(R.id.search_youtube);
		ImageButton ui_google = (ImageButton) findViewById(R.id.search_google);
		ImageButton ui_daum = (ImageButton) findViewById(R.id.search_daum);
		ImageButton ui_naver = (ImageButton) findViewById(R.id.search_naver);
		ImageButton ui_bing = (ImageButton) findViewById(R.id.search_bing);
		ImageButton ui_nate = (ImageButton) findViewById(R.id.search_nate);
		TextView ui_close = (TextView) findViewById(R.id.search_close);

		final Spinner ui_keyword = (Spinner) this.findViewById(R.id.search_keyword);
		final String[] keywords = getContext().getResources().getStringArray(R.array.search_keyword);
		final String[] keywordsValues = getContext().getResources().getStringArray(R.array.search_keyword_value);
		ArrayAdapter<String> keywordAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, keywords);
		keywordAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ui_keyword.setAdapter(keywordAdapter);
		ui_keyword.setPrompt(getContext().getResources().getString(R.string.search_keyword_prompt));

		ui_youtube.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_YOUTUBE, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
				
//				Fragment newContent = new WebViewPage();
//				Bundle bundle = new Bundle();
//				bundle.putString(WebViewPage.URL, "http://m.youtube.com/results?gl=RU&client=mv-google&hl=en&q=" + keyword + "&submit=Search");
//				newContent.setArguments(bundle);
//				if (newContent != null) {
//					if (activity instanceof MainActivity) {
//						MainActivity ma = (MainActivity) activity;
//						ma.switchContent(newContent, activity.getResources().getString(R.string.search_youtube));
//					}
//				}
			}
		});

		ui_google.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_GOOGLE, keyword, keyword, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
			}
		});

		ui_daum.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_DAUM, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
				
//				Fragment newContent = new WebViewPage();
//				Bundle bundle = new Bundle();
//				bundle.putString(WebViewPage.URL, "http://m.search.daum.net/search?w=tot&nil_mtopsearch=btn&q=" + keyword);
//				newContent.setArguments(bundle);
//				if (newContent != null) {
//					if (activity instanceof MainActivity) {
//						MainActivity ma = (MainActivity) activity;
//						ma.switchContent(newContent, activity.getResources().getString(R.string.search_daum));
//					}
//				}
			}
		});

		ui_naver.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_NAVER, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
				
//				Fragment newContent = new WebViewPage();
//				Bundle bundle = new Bundle();
//				bundle.putString(WebViewPage.URL, "http://m.search.naver.com/search.naver?query=" + keyword + "&where=m&sm=mtp_hty");
//				newContent.setArguments(bundle);
//				if (newContent != null) {
//					if (activity instanceof MainActivity) {
//						MainActivity ma = (MainActivity) activity;
//						ma.switchContent(newContent, activity.getResources().getString(R.string.search_naver));
//					}
//				}
			}
		});

		ui_bing.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_BING, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));

//				Fragment newContent = new WebViewPage();
//				Bundle bundle = new Bundle();
//				bundle.putString(WebViewPage.URL, "http://m.bing.com/search?q=" + keyword + "&FORM=BLXBSS&btsrc=internal");
//				newContent.setArguments(bundle);
//				if (newContent != null) {
//					if (activity instanceof MainActivity) {
//						MainActivity ma = (MainActivity) activity;
//						ma.switchContent(newContent, activity.getResources().getString(R.string.search_bing));
//					}
//				}
			}
		});
		
		ui_nate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Search.this.dismiss();
				String keyword = keywordsValues[ui_keyword.getSelectedItemPosition()];
				
				Uri uri = Uri.parse(String.format(Constants.SEARCH_URL_NATE, keyword));
				activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));

//				Fragment newContent = new WebViewPage();
//				Bundle bundle = new Bundle();
//				bundle.putString(WebViewPage.URL, "http://m.bing.com/search?q=" + keyword + "&FORM=BLXBSS&btsrc=internal");
//				newContent.setArguments(bundle);
//				if (newContent != null) {
//					if (activity instanceof MainActivity) {
//						MainActivity ma = (MainActivity) activity;
//						ma.switchContent(newContent, activity.getResources().getString(R.string.search_bing));
//					}
//				}
			}
		});

		ui_close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Search.this.dismiss();
			}
		});
	}
}
